<?php

namespace App\Admin\Forms;

use App\Models\OrderService;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;


class IsCarefree extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能

    // 处理请求
    public function handle(array $input)
    {
        $id = $this->payload['id'] ?? null;
        $arr = json_decode(OrderService::where('order_id', $id)->value('submit_reg_info'), true);
        foreach ($arr as $key => &$val) {
            // var_dump($val);
            if ($val['id'] == $input['submit_reg_info']) {
                $val['submit_reg_info_periodical_list'] = $input['submit_reg_info_periodical_list'];
            }
        }
       

        if (!$id) {
            return $this->response()->error('参数错误');
        }

        $result = OrderService::where('order_id', $id)->update(['submit_reg_info' => json_encode($arr)]);

        if ($result) {
            // OrderService::create(['order_id' => $result->id]);
            return $this->response()->success('成功')->refresh();
        } else {
            return $this->response()->error('失败');
        }
    }

    public function form()
    {
        $id = $this->payload['id'] ?? null;
        $options = [];
        if ($id) {
            $arr = json_decode(OrderService::where('order_id', $id)->value('submit_reg_info'), true);

            $options = array_column($arr, 'submit_reg_info_system_name', 'id');
        }
        // 获取外部传递参数
        $this->select('submit_reg_info', '投稿注册信息')->options($options);
        $this->array('submit_reg_info_periodical_list', '投稿目标期刊', function ($table) {
            $table->text('submit_reg_info_periodical', '期刊名');
            $table->text('submit_reg_info_influence_factor', '影响因子');
            $table->select('submit_reg_info_status', '投稿状态')->when([2, 3], function (Form $form) {
                $form->file('upload', '文件')->saveFullUrl()->autoUpload();
            })->options([1 => '投稿中', 2 => '已接收', 3 => '被拒绝']);
        });
    }

    // 返回表单数据，如不需要可以删除此方法
    public function
    default()
    {
        return [
            'submit_reg_info'         => '', // 订单编号

        ];
    }
}
